<?php

namespace app\common\model;


use think\Exception;

class MallGoodsSku extends Base
{

    protected $deleteTime = false;


    /**
     * 根据商品id 获取所有规格
     * @param int $goods_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getGoodsSkuBySpecs(int $goods_id = 0): array
    {
        return $this->field('*,specs_value_ids as get_specs')
            ->where('goods_id', $goods_id)
            ->select()
            ->toArray();
    }

    /**
     * @param $value
     * @return string todo 颜色：红色
     */
    protected function getGetSpecsAttr($value): string
    {
        // 1-6
        $arr = explode('-', $value);
        if (empty($arr)) {
            return "";
        } else {
            $str = "";
            foreach ($arr as $v) {
                $field = "s.id as s_id,s.specs_id as s_specs_id,s.title as s_title,p.title as p_title,p.id as p_id";
                $ret = (new MallSpecsValue())->field($field)
                    ->alias('s')
                    ->join('mall_specs p', 's.specs_id = p.id')
                    ->where('s.id', $v)
                    ->find();
                if ($ret) {
                    $str .= $ret['p_title'] . ":" . $ret['s_title'] . "-";
                }
            }
            return rtrim($str, '-');
        }
    }

    /**
     * @param int $id
     * @return array|string
     * @throws Exception
     */
    public function getGoodsSkuBySpecsValue(int $id = 0)
    {
        try {
            $row = $this->getBaseById($id);
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        //获取规格
        if (empty($row) || empty($row['specs_value_ids'])) {
            return "-";
        }
        $tags = explode('-', $row['specs_value_ids']);
        if (empty($tags)) {
            return "-";
        }
        try {
            $lists = (new MallSpecsValue())->getSpecsValueByIdValue($tags);
        } catch (\Exception $e) {
            throw new Exception($e->getMessage());
        }
        return $lists;

    }

    /**
     * @param int $goods_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMallGoodsSkuByValues(int $goods_id = 0): array
    {
        return $this
            ->where('goods_id', $goods_id)
            ->select()
            ->toArray();
    }

    /**
     * @param int $id
     * @return array
     * @throws Exception
     */
    public function getGoodsByDetail(int $id = 0)
    {
        try {
            $row = $this
                ->with(['goods'])
                ->where('id', $id)
                ->where('status', 1)
                ->find();
        } catch (\Exception $e) {
            throw new Exception("商品信息不存在");
        }
        if (!$row) {
            return [];
        }
        return $row->toArray();
    }
    public function shop(){
        return $this->hasOne(MallShop::class,'id','shop_id');
    }

    /**
     * @param int $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSkuByGoodsId(int $id = 0): array
    {
        if ($id == 0) {
            return [];
        }
        return $this
            ->where('goods_id', $id)
            ->where('status', 1)
            ->select()
            ->toArray();
    }

    /**
     * 根据商品id 获取商品信息
     * @return \think\model\relation\HasOne
     */
    protected function goods()
    {
        return $this->hasOne(MallGoods::class, 'id', 'goods_id');
    }

    /**
     * @param int $sku_id
     * @param bool $inc true为增加 false 减少
     * @param int $num
     * @return mixed
     */
    public function setStockByDec(int $sku_id = 0, bool $inc = true, int $num = 1)
    {
        if ($inc) {
            return $this->where('id', $sku_id)
                ->inc('stock', $num)
                ->update();

        } else {
            return $this->where('id', $sku_id)
                ->dec('stock', $num)
                ->update();
        }
    }

    /**
     * @param array $data
     * @return bool
     * @throws Exception
     */
    public function updateStockByData(array $data = []): bool
    {
        try {
            foreach ($data as $v) {
                $this->setStockByDec($v['option_id'], false, $v['total']);
            }
        }catch (\Exception $e){
            throw new Exception("操作失败");
        }
        return true;
    }

    /**
     * @param int $goods_id
     * @return MallGoodsSku|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getGoodsSkuByGoodsId(int $goods_id = 0)
    {
        return $this->where('goods_id',$goods_id)
            ->find();
    }

}